from app import db
from datetime import datetime

# 食谱模型
class Recipe(db.Model):
    __tablename__ = 'recipes'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False)
    difficulty = db.Column(db.Enum('简单', '中等', '困难'), nullable=False)
    cook_time = db.Column(db.Integer, nullable=False)
    servings = db.Column(db.Integer)
    cover_image = db.Column(db.String(255))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 唯一约束：防止同一用户创建相同标题的菜谱
    __table_args__ = (db.UniqueConstraint('user_id', 'title'),)
    
    # 关系定义
    user = db.relationship('User', backref=db.backref('recipes', lazy=True))
    category = db.relationship('Category', backref=db.backref('recipes', lazy=True))
    recipe_ingredients = db.relationship('RecipeIngredient', lazy=True, overlaps="ingredients_list,ingredients,recipe_items,recipe")
    ingredients = db.relationship('Ingredient', secondary='recipe_ingredients', overlaps="recipe,recipe_ingredients,recipe_items,ingredients_list")
    steps = db.relationship('RecipeStep', backref='recipe', lazy=True)
    comments = db.relationship('Comment', backref='recipe', lazy=True)
    favorites = db.relationship('Favorite', backref='recipe', lazy=True)
    tags = db.relationship('Tag', secondary='recipe_tags', backref=db.backref('recipes', lazy=True))
    
    def to_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'description': self.description,
            'user_id': self.user_id,
            'category_id': self.category_id,
            'difficulty': self.difficulty,
            'cook_time': self.cook_time,
            'servings': self.servings,
            'cover_image': self.cover_image,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }

# 食谱步骤模型
class RecipeStep(db.Model):
    __tablename__ = 'recipe_steps'
    
    id = db.Column(db.Integer, primary_key=True)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'), nullable=False)
    step_number = db.Column(db.Integer, nullable=False)
    description = db.Column(db.Text, nullable=False)
    image_url = db.Column(db.String(255))
    
    def to_dict(self):
        return {
            'id': self.id,
            'recipe_id': self.recipe_id,
            'step_number': self.step_number,
            'description': self.description,
            'image_url': self.image_url
        }

# 食谱食材关联模型
class RecipeIngredient(db.Model):
    __tablename__ = 'recipe_ingredients'
    
    id = db.Column(db.Integer, primary_key=True)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'), nullable=False)
    ingredient_id = db.Column(db.Integer, db.ForeignKey('ingredients.id'), nullable=False)
    quantity = db.Column(db.Numeric(10, 2))
    unit = db.Column(db.String(20))
    
    # 关系定义
    recipe = db.relationship('Recipe', overlaps="recipe_ingredients,ingredients,ingredients_list,recipe_items")
    ingredient = db.relationship('Ingredient', overlaps="ingredients,recipe_items,recipe_ingredients,ingredients_list")
    
    # 唯一约束
    __table_args__ = (db.UniqueConstraint('recipe_id', 'ingredient_id'),)
    
    def to_dict(self):
        return {
            'id': self.id,
            'recipe_id': self.recipe_id,
            'ingredient_id': self.ingredient_id,
            'quantity': float(self.quantity) if self.quantity else None,
            'unit': self.unit
        }

# 食谱标签关联模型
class RecipeTag(db.Model):
    __tablename__ = 'recipe_tags'
    
    id = db.Column(db.Integer, primary_key=True)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'), nullable=False)
    tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), nullable=False)
    
    # 唯一约束
    __table_args__ = (db.UniqueConstraint('recipe_id', 'tag_id'),)
    
    def to_dict(self):
        return {
            'id': self.id,
            'recipe_id': self.recipe_id,
            'tag_id': self.tag_id
        }